﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Article
{
    public class Category : BaseArticle
    {
        #region "Constructors"
        public Category(int categoryID, DateTime addedDate, string addedBy, string title, int importance, string description, string imageUrl)
        {
            this.ID = categoryID;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Title = title;
            this.Importance = importance;
            this.Description = description;
            this.ImageUrl = imageUrl;
        }
        #endregion

        #region "Public Properties"
        public string Title { get; set; }

        public int Importance { get; set; }

        public string Description { get; set; }

        public string ImageUrl { get; set; }
        #endregion

        #region Get From CategoryDetail
        private static Category GetCategoryFromCategoryDetails(CategoryDetails record)
        {
            if (record == null)
                return null;
            else
            {
                return new Category(record.ID, record.AddedDate, record.AddedBy,
                   record.Title, record.Importance, record.Description, record.ImageUrl);
            }
        }

        private static List<Category> GetCategoryListFromCategoryDetailsList(List<CategoryDetails> recordset)
        {
            List<Category> categories = new List<Category>();
            foreach (CategoryDetails record in recordset)
                categories.Add(GetCategoryFromCategoryDetails(record));
            return categories;
        }

        #endregion

        #region Static Method
        public static List<Category> ListCategories()
        {
            List<Category> categories = null;
            string key = "Categories";

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                categories = (List<Category>)BizObject.Cache[key];
            }
            else
            {
                List<CategoryDetails> recordset = SiteProvider.Articles.ListCategories();
                categories = GetCategoryListFromCategoryDetailsList(recordset);
                BaseArticle.CacheData(key, categories);
            }
            return categories;
        }

        public static Category GetCategoryByID(int categoryID)
        {
            Category category = null;
            string key = "Category_" + categoryID.ToString();

            if (BaseArticle.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                category = (Category)BizObject.Cache[key];
            }
            else
            {
                category = GetCategoryFromCategoryDetails(SiteProvider.Articles.GetCategoryByID(categoryID));
                BaseArticle.CacheData(key, category);
            }
            return category;
        }

        public static bool UpdateCategory(int id, string title, int importance, string description, string imageUrl)
        {
            CategoryDetails record = new CategoryDetails(id, DateTime.Now, "", title, importance, description, imageUrl);
            bool ret = SiteProvider.Articles.UpdateCategory(record);
            BizObject.PurgeCacheItems("Categor");
            return ret;
        }

        public static bool DeleteCategory(int id)
        {
            bool ret = SiteProvider.Articles.DeleteCategory(id);
            //new RecordDeletedEvent("category", id, null).Raise();
            BizObject.PurgeCacheItems("Categor");
            return ret;
        }

        public static int InsertCategory(string title, int importance, string description, string imageUrl)
        {
            CategoryDetails record = new CategoryDetails(0, DateTime.Now,
               BizObject.CurrentUserName, title, importance, description, imageUrl);
            int ret = SiteProvider.Articles.InsertCategory(record);
            BizObject.PurgeCacheItems("Categor");
            return ret;
        }
        #endregion

        #region Method
        public bool Delete()
        {
            bool success = Category.DeleteCategory(this.ID);
            if (success)
                this.ID = 0;
            return success;
        }

        public bool Update()
        {
            return Category.UpdateCategory(this.ID, this.Title, this.Importance, this.Description, this.ImageUrl);
        }

        #endregion
    }
}
